<?php

namespace app\admin\controller;
use think\db\Query;
use api\mall\model\UserAuditModel;
use cmf\controller\AdminBaseController;

class AuditController extends AdminBaseController
{
    public function index()
    {
        $filter = $this->request->param();
        $startTime = isset($filter['start_time']) ? $filter['start_time'] : '';
        $endTime = isset($filter['end_time']) ? $filter['end_time'] : '';
        if (!empty($startTime) && !empty($endTime) && $startTime >= $endTime) {
            // $this->error("截止日期需大于起始日期");
            $tempTime = $filter['start_time'];
            $filter['start_time'] = $filter['end_time'];
            $filter['end_time'] = $tempTime;
        }
        
        $userAuditModel = new UserAuditModel();
        $auditQuery = $userAuditModel->alias('a');
        $auditQuery->join('user u', 'a.user_id = u.id');

        $auditDatas = $auditQuery->field('a.*, u.mobile')
            ->where(function (Query $query) use ($filter) {
                $startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
                $endTime   = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
                if (!empty($startTime) && !empty($endTime) && $startTime >= $endTime) {
                    $tempTime = $startTime;
                    $startTime = $endTime;
                    $endTime = $tempTime;
                }
                if (!empty($startTime)) {
                    $query->where('a.create_time', '>=', $startTime);
                }
                if (!empty($endTime)) {
                    $endTime = $endTime + 86400;
                    $query->where('a.create_time', '<=', $endTime);
                }

                $userId = empty($filter['user_id']) ? 0 : intval($filter['user_id']);
                if (!empty($userId)) {
                    $query->where('a.user_id', $userId);
                }

                $mobile = empty($filter['mobile']) ? 0 : intval($filter['mobile']);
                if (!empty($mobile)) {
                    $query->where('u.mobile', $mobile);
                }

                $auditStatus = empty($filter['audit_status']) ? 0 : intval($filter['audit_status']);
                if (!empty($auditStatus)) {
                    $query->where('a.audit_status', $auditStatus);
                }
            })
            ->order('create_time', 'DESC')
            ->paginate(20);

        $this->assign('start_time', isset($filter['start_time']) ? $filter['start_time'] : '');
        $this->assign('end_time', isset($filter['end_time']) ? $filter['end_time'] : '');
        $this->assign('user_id', isset($filter['user_id']) ? $filter['user_id'] : '');
        $this->assign('mobile', isset($filter['mobile']) ? $filter['mobile'] : '');
        $this->assign('audit_status', isset($filter['audit_status']) ? $filter['audit_status'] : '');

        $this->assign('page', $auditDatas->render());
        $this->assign('items', $auditDatas);
        return $this->fetch();
    }

    public function detail()
    {
        $id = $this->request->param("id", 0, 'intval');
        if ($id <= 0) {
            $this->error("无效的参数！");
        }
        
        $mobile = $this->request->param("mobile");
        $this->assign('mobile', $mobile);

        $model = new UserAuditModel();
        $data = $model->where('id', $id)->select();
        $this->assign('data', $data[0]);

        return $this->fetch();
    }
    
    // public function doAudit()
    // {
    //     if ($this->request->post()) {
    //         $model = new UserAuditModel();
    //         $data = $this->request->post();
    //         if (empty($data['id'])) {
    //             return $this->fail('无效的记录ID');
    //         } 
    //         else {
    //             $update = [];
    //             $update['audit_status'] = $data['audit_status'];
    //             $update['update_time'] = time();
    //             $model->where('id', $data['id'])->update($update);
    //         }
    //         return $this->success('ok');
    //     } else {
    //         return $this->fail('参数错误');
    //     }
    // }
}
